//
//  SLRecomendViewController.m
//  StarLight
//
//  Created by 吴涛 on 16/6/5.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLRecomendViewController.h"
#import "SLDeployRecomViewModel.h"
#import "SLDeployRecomendCell.h"
#import "SLProductDetailViewController.h"
#import "SLBoatDetailViewController.h"
#import "SLMapViewController.h"

@interface SLRecomendViewController ()<UICollectionViewDelegate, UICollectionViewDataSource,SLDeployRecomendCellDelegate>

@property (nonatomic, strong) UICollectionView *recCollectionView;

@property (nonatomic, strong) SLDeployRecomViewModel *reVM;

@property (nonatomic, strong) NSString *bid;

@property (nonatomic, strong) NSString *gid;

@end

@implementation SLRecomendViewController


- (instancetype)initWithBid:(NSString *)bid{
  if (self = [super init]) {

    self.bid = bid;
    self.reVM = [[SLDeployRecomViewModel alloc] initWithBid:bid];
    self.title = @"推荐货源";
  }
  return self;
}

- (instancetype)initWithGid:(NSString *)gid{
  if (self = [super init]) {
    self.gid = gid;
    self.reVM = [[SLDeployRecomViewModel alloc] initWithGid:gid];
    self.title = @"推荐船舶";
  }
  return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
  self.view.backgroundColor = [UIColor defeutBackgroudColor];
  [self setUpUI];
  [self addObservers];
  
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
  [super viewWillAppear:animated];
  [self.navigationController setNavigationBarHidden:NO animated:YES];
  
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)setUpUI{
  [self.view addSubview:self.recCollectionView];
  [self.recCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.width.equalTo(self.view);
    make.bottom.equalTo(self.mas_bottomLayoutGuideTop);
    make.top.equalTo(self.view).offset(10);
  }];
  
  UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"清空" style:UIBarButtonItemStylePlain target:self action:@selector(canCleanAll)];
  self.navigationItem.rightBarButtonItem = rightItem;
}

/**
 *  清空提示
 */
- (void)canCleanAll{
  @weakify(self)
  UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定清空所有推荐吗?" preferredStyle:UIAlertControllerStyleAlert];
  UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    @strongify(self)
    [self cleanAll];
  }];
  
  UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    
  }];
  [alertVC addAction:confirmAction];
  [alertVC addAction:cancelAction];
  
  [self presentViewController:alertVC animated:YES completion:nil];
  
}

- (void)cleanAll{
  @weakify(self)
  if(self.gid){
    
    [self.reVM deleteAllShipWithGid:self.gid block:^{
      @strongify(self)
      
      [[NSNotificationCenter defaultCenter] postNotificationName:kDeletAllSuccess object:nil];
      [SVProgressHUD showSuccessWithStatus:@"清空推荐船舶成功"];
      [self.navigationController popViewControllerAnimated:YES];
    }];
  }else if(self.bid){
    
    [self.reVM deleteAllGoodsWithbid:self.bid block:^{
      @strongify(self)
      [[NSNotificationCenter defaultCenter] postNotificationName:kDeletAllSuccess object:nil];
      [SVProgressHUD showSuccessWithStatus:@"清空推荐货源成功"];
      [self.navigationController popViewControllerAnimated:YES];
    }];
  }
}

- (void)addObservers{
  @weakify(self)
  [[RACObserve(self.reVM, recomList) filter:^BOOL(id value) {
    return value != nil;
  }] subscribeNext:^(id x) {
    @strongify(self)
    [self.recCollectionView reloadData];
  }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - SLDeployRecomendCellDelegate

- (void)cellDelectBtnWasTapedWithShipModel:(SLNearLyShip *)model{
 
  @weakify(self)
  [self.reVM deleteShipWithBid:model.bid block:^{
    @strongify(self)
    [self.reVM first];
  }];
}

- (void)cellDelectBtnWasTapedWithGoodsModel:(SLNearLyProduct *)model{
  @weakify(self)
  [self.reVM deleteGoodsWithGid:model.gid block:^{
    @strongify(self)
    [self.reVM first];
  }];
}

- (void)cellLocationBtnWasTapedWithShipModel:(SLNearLyShip *)model{
  SLMapViewController *mapVC = [[SLMapViewController alloc] init];
  [mapVC locationWithLon:model.shiplon lat:model.shiplat];
  [self.navigationController pushViewController:mapVC animated:YES];
}



- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{
  if (!self.reach.isReachable) {
    return [[NSAttributedString alloc] initWithString:@"无网络连接, 请连接网络后刷新" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
  }
  if(self.reVM.type == 0){
    return [[NSAttributedString alloc] initWithString:@"暂无推荐货源" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
  }else{
    return [[NSAttributedString alloc] initWithString:@"暂无推荐船舶" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
  }
  
}


#pragma mark - UICollectionViewDelegate, UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  return  self.reVM.recomList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  
  SLDeployRecomendCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  
  if (self.reVM.type == 0) {
    [cell configWithType:NearlyCellTypeShip];
  }else{
    [cell configWithType:NearlyCellTypeProduct];
  }
  cell.recDelegate = self;
  return cell;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(SLDeployRecomendCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
  
  if (self.reVM.type == 0) {
    [cell configNearlyCellWithProductModel:self.reVM.recomList[indexPath.row]];
  }else{
    [cell configNearlyCellWithShipModel:self.reVM.recomList[indexPath.row]];
  }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  
  
  
  if (self.reVM.type == 0 ) {
    // 如果是船 则要显示 推荐货物信息
    SLNearLyProduct *model = self.reVM.recomList[indexPath.row];
    SLProductDetailViewController *productDVC = [SLProductDetailViewController new];
    [productDVC changeGid:model.gid];
    [self.navigationController pushViewController:productDVC animated:YES];
  }else if (self.reVM.type == 1){
    // 如果是货主 则要显示 船详细信息
    SLNearLyShip *model = self.reVM.recomList[indexPath.row];;
    SLBoatDetailViewController *boatDVC = [SLBoatDetailViewController new];
    [boatDVC changeBid:model.bid];
    [self.navigationController pushViewController:boatDVC animated:YES];
  }
  
  
  
}



- (UICollectionView *)recCollectionView{
  if (!_recCollectionView) {
    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH-20, 140);
    layout.minimumLineSpacing = 2.5f;
    _recCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    _recCollectionView.backgroundColor = [UIColor whiteColor];
    _recCollectionView.backgroundColor = [UIColor defeutBackgroudColor];
    [_recCollectionView registerClass:[SLDeployRecomendCell class] forCellWithReuseIdentifier:@"cell"];
    _recCollectionView.delegate = self;
    _recCollectionView.dataSource = self;
    _recCollectionView.emptyDataSetSource = self;
    _recCollectionView.emptyDataSetDelegate = self;
  }
  return _recCollectionView;
}





/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
